package com.hsnn.medstgmini.pro.project.validator;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hsnn.medstgmini.pro.project.model.BidProject;
import com.hsnn.medstgmini.pro.project.service.BidProjectManager;
import com.hsnn.medstgmini.validator.Result;

/**
 * @category 新增项目信息校验
 * @author 韩守松
 * @date 2015年7月13日
 */
@Component
@Scope("singleton")
public class BidProjectValidator {
	private static final Logger log = Logger
			.getLogger(BidProjectValidator.class);

	private static BidProjectManager bidProjectManager;

	public static void setBidProjectManager(BidProjectManager bidProjectManager) {
		BidProjectValidator.bidProjectManager = bidProjectManager;
	}

	public static Result validate(BidProject bidProject) {
		Result validate = new Result();
		List<BidProject> list = new ArrayList<BidProject>();
		Map<String, List<String>> failCauseMap = new HashMap<String, List<String>>();
		List<String> failDescriptionList = new ArrayList<String>();
		boolean falg = false;
		//String regEx1 = "^[A-Z]{1}[0-9]{6}";//校验项目编号
		String regEx1 = "[^\u4e00-\u9fa5]";
		String regDate= "(\\d{4})-(\\d{1,2})-(\\d{1,2})";//校验时间
		// 校验项目编号
		if (bidProjectManager != null) {
			if (StringUtils.isNotEmpty(bidProject.getProjCode())) {
				if (!Pattern.compile(regEx1).matcher(bidProject.getProjCode()).matches()) {
					if (bidProject.getProjCode().length() <= 18 ) {
						list = bidProjectManager
								.getProjectListByProjectCode(bidProject);
						if (list.size() > 0) {
							failDescriptionList.add("项目编号已存在</br>");
							failCauseMap.put("项目编号不正确", failDescriptionList);
							validate.setSuccess(falg);
						} else {
							failDescriptionList.add("");
							failCauseMap.put("项目编号不正确", failDescriptionList);
							validate.isSuccess();
						}
					}
				} else {
					failDescriptionList.add("项目编号格式错误");
					failCauseMap.put("项目编号不正确", failDescriptionList);
					validate.setSuccess(falg);
				}
				if (bidProject.getProjCode().length() > 18) {
					failDescriptionList.add("项目编号格式错误");
					failCauseMap.put("项目编号不正确", failDescriptionList);
					validate.setSuccess(falg);
				}
				/*if (bidProject.getProjCode().length() > 7) {
					failDescriptionList.add("项目编号格式错误：正确格式P000000(一个大写字符+6个数字)");
					failCauseMap.put("项目编号不正确", failDescriptionList);
					validate.setSuccess(falg);
				}*/
			} else {
				failDescriptionList.add("</br>项目编号不能为空");
				failCauseMap.put("项目编号不正确", failDescriptionList);
				validate.setSuccess(falg);
			}
			validate.setFailCauseMap(failCauseMap);
			
			//校验项目名称
			failDescriptionList = new ArrayList<String>();
			if (StringUtils.isNotEmpty(bidProject.getName())) {
				if (bidProject.getProjCode().length() <= 128) {
					list = bidProjectManager
							.getProjectListByProjectName(bidProject);
					if (list.size() > 0) {
						failDescriptionList.add("项目名称已存在</br>");
						failCauseMap.put("项目名称错误", failDescriptionList);
						validate.setSuccess(falg);
					} else {
						failDescriptionList.add("");
						failCauseMap.put("项目名称错误", failDescriptionList);
						validate.isSuccess();
					}
				} else {
					failDescriptionList.add("项目名称最大长度为128字符");
					failCauseMap.put("项目名称错误", failDescriptionList);
					validate.setSuccess(falg);
				}
			} else {
				failDescriptionList.add("</br>项目名称不能为空");
				failCauseMap.put("项目名称错误", failDescriptionList);
				validate.setSuccess(falg);
			}
			validate.setFailCauseMap(failCauseMap);
			
			//校验项目类型
			failDescriptionList = new ArrayList<String>();
			if(StringUtils.isNotEmpty(bidProject.getProjCat())){
				if("1".equals(bidProject.getProjCat())||"2".equals(bidProject.getProjCat())||"3".equals(bidProject.getProjCat())||"4".equals(bidProject.getProjCat())){
					failDescriptionList.add("");
					failCauseMap.put("项目类型错误", failDescriptionList);
					validate.isSuccess();
				}else{
					failDescriptionList.add("项目类型错误</br>");
					failCauseMap.put("项目类型错误", failDescriptionList);
					validate.setSuccess(falg);
				}
			}else{
				failDescriptionList.add("项目类型不能为空");
				failCauseMap.put("项目类型错误", failDescriptionList);
				validate.setSuccess(falg);
			}
			validate.setFailCauseMap(failCauseMap);
			
			//校验项目状态
			failDescriptionList = new ArrayList<String>();
			if(null != bidProject.getStatus() && !"".equals(bidProject.getStatus())){
				if(1 == bidProject.getStatus()|| 0 == bidProject.getStatus()){
					failDescriptionList.add("");
					failCauseMap.put("项目状态错误", failDescriptionList);
					validate.isSuccess();
				}else{
					failDescriptionList.add("项目状态错误</br>");
					failCauseMap.put("项目状态错误", failDescriptionList);
					validate.setSuccess(falg);
				}
			}else{
				failDescriptionList.add("项目状态不能为空");
				failCauseMap.put("项目状态错误", failDescriptionList);
				validate.setSuccess(falg);
			}
			validate.setFailCauseMap(failCauseMap);
			
			//校验项目开始时间
			failDescriptionList = new ArrayList<String>();
			if(null != bidProject.getStarttime() && !"".equals(bidProject.getStarttime())){
		     
				if(Pattern.compile(regDate).matcher(new SimpleDateFormat("yyyy-MM-dd").format(bidProject.getStarttime())).matches()){
					failDescriptionList.add("");
					failCauseMap.put("项目开始时间错误", failDescriptionList);
					validate.isSuccess();
				}else{
					failDescriptionList.add("项目采购开始时间填写错误</br>");
					failCauseMap.put("项目开始时间错误", failDescriptionList);
					validate.setSuccess(falg);
				}
			}else{
				failDescriptionList.add("项目采购开始时间不能为空");
				failCauseMap.put("项目开始时间错误", failDescriptionList);
				validate.setSuccess(falg);
			}
			validate.setFailCauseMap(failCauseMap);
			
			//校验项目结束时间
			failDescriptionList = new ArrayList<String>();
			if(null != bidProject.getEndtime() && !"".equals(bidProject.getEndtime())){
		     
				if(Pattern.compile(regDate).matcher(new SimpleDateFormat("yyyy-MM-dd").format(bidProject.getEndtime())).matches()){
					if(bidProject.getEndtime().getTime() > bidProject.getStarttime().getTime()){
						failDescriptionList.add("");
						failCauseMap.put("项目结束时间错误", failDescriptionList);
						validate.isSuccess();
					}else{
						failDescriptionList.add("项目采购结束时间必须大于采购开始时间</br>");
						failCauseMap.put("项目结束时间错误", failDescriptionList);
						validate.setSuccess(falg);
					}
				}else{
					failDescriptionList.add("项目采购结束时间填写错误");
					failCauseMap.put("项目结束时间错误", failDescriptionList);
					validate.setSuccess(falg);
				}
			}else{
				failDescriptionList.add("</br>项目采购结束时间不能为空");
				failCauseMap.put("项目结束时间错误", failDescriptionList);
				validate.setSuccess(falg);
			}
			validate.setFailCauseMap(failCauseMap);
			
			//校验项目简介
			failDescriptionList = new ArrayList<String>();
			if(StringUtils.isNotEmpty(bidProject.getProjIntro())){
		     
				if(bidProject.getProjIntro().length() <= 2000){
					failDescriptionList.add("");
					failCauseMap.put("项目简介错误", failDescriptionList);
					validate.isSuccess();
				}else{
					failDescriptionList.add("项目简介最大长度填写2000个字符</br>");
					failCauseMap.put("项目简介错误", failDescriptionList);
					validate.setSuccess(falg);
				}
			}else{
				failDescriptionList.add("项目简介不能为空");
				failCauseMap.put("项目简介错误", failDescriptionList);
				validate.setSuccess(falg);
			}
			validate.setFailCauseMap(failCauseMap);
			
			//校验项目备注
			failDescriptionList = new ArrayList<String>();
			if(StringUtils.isNotEmpty(bidProject.getRemark())){
		     
				if(bidProject.getRemark().length() <= 2000){
					failDescriptionList.add("");
					failCauseMap.put("项目备注错误", failDescriptionList);
					validate.isSuccess();
				}else{
					failDescriptionList.add("项目备注最大长度填写2000个字符</br>");
					failCauseMap.put("项目备注错误", failDescriptionList);
					validate.setSuccess(falg);
				}
			}else{
				failDescriptionList.add("项目简介不能为空");
				failCauseMap.put("项目备注错误", failDescriptionList);
				validate.setSuccess(falg);
			}
			validate.setFailCauseMap(failCauseMap);
		} else {
			log.error("bidProjectManager is null.");
		}
		return validate;
	}
}
